package com.entrogy.controller;

import com.entrogy.bean.StudentEntity;
import com.entrogy.service.StudentService;
import com.entrogy.timing.TimingTaskService;
import com.entrogy.utils.ApiReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author xlr
 * @date 2019-06
 * @description 推送时间功能模块-----所有学校一起
 */
@Api(description = "推送时间的功能模块包含推送个人和年级还有全部（注：目前只给我们内部使用哟）")
@RestController
@RequestMapping(value = "/pushtime")
public class PushTimeController {

    private static final Logger logger = LoggerFactory.getLogger(PushTimeController.class);

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "推送所有学校的当天通行权限")
    @PostMapping(value = "/pushtoday")
    public Map<String,Object> pushToday(){
        try {
            new TimingTaskService().createTableAndPushTime();
        } catch (ParseException e) {
            logger.info("推送所有学校当天的通行权限失败"+e.getMessage());
            return ApiReturnUtil.error("推送所有学校当天的通行权限失败");
        }
        return ApiReturnUtil.success("推送成功咦",null);
    }

    @ApiOperation(value = "推送某个学生的当天通行权限")
    @PostMapping(value = "/pushstudent")
    public Map<String,Object> pushStudent(int studentId){
        StudentEntity studentEntity = studentService.getStudentById(studentId); // 学生 ID
        if (studentEntity == null){
            return ApiReturnUtil.error("抱歉，没有该学生！");
        }
        new TimingTaskService().pushTime(studentEntity);// 推某个 学生的权限时间
        logger.info("推送单个 学生的权限 执行 完毕 ==>" + studentEntity.getStudentName());
        return ApiReturnUtil.success("推送个人权限成功咦",null);
    }

    @ApiOperation(value = "推送学校某个年级的当天通行权限")
    @PostMapping(value = "/pushgrade")
    public Map<String,Object> pushGrade(String gradeId, Integer schoolCode){
        List<StudentEntity> studentEntities = studentService.getStudentByGrade(gradeId, schoolCode);
        return pushStudentListTime(gradeId, studentEntities);
    }

    @ApiOperation(value = "推送学校某个班级的当天通行权限")
    @PostMapping(value = "/pushClass")
    public Map<String,Object> pushClass(String classId, Integer schoolCode){
        List<StudentEntity> studentEntities = studentService.listStudentByClassId(classId, schoolCode);
        return pushStudentListTime(classId, studentEntities);
    }

    private Map<String, Object> pushStudentListTime(String gradeId, List<StudentEntity> studentEntities) {
        for (StudentEntity studentEntity : studentEntities) {
            if (studentEntity.getStudentThdid() == 0){
                logger.info("学生=>" + studentEntity.getStudentName() + "未录入照片，不推送权限！");
                continue;
            }
            new TimingTaskService().pushTime(studentEntity);// 推某个 学生的权限时间
            logger.info("推送年级/班级" + gradeId + "" + studentEntity.getStudentName() + "当天的通行权限成功");
        }
        return ApiReturnUtil.success("推送年级权限成功咦", null);
    }

}
